package cn.edu.nuc.carsystem.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.nuc.carsystem.dto.PageInfo;
import cn.edu.nuc.carsystem.model.Bank;
import cn.edu.nuc.carsystem.model.City;
import cn.edu.nuc.carsystem.model.Conrtuny;
import cn.edu.nuc.carsystem.model.Custome;
import cn.edu.nuc.carsystem.model.Loan;
import cn.edu.nuc.carsystem.model.SysUser;
import cn.edu.nuc.carsystem.service.interfaces.BankService;
import cn.edu.nuc.carsystem.service.interfaces.CityService;
import cn.edu.nuc.carsystem.service.interfaces.CustomeService;
import cn.edu.nuc.carsystem.service.interfaces.LoanService;
/**
 * 贷款controller
 * @author 杨晓庆
 *
 */
@RequestMapping("/custome/loan")
@Controller
public class LoanController {
   @Autowired
   private LoanService loanService;
   @Autowired
   private BankService bankService;
   @Autowired
   private CustomeService customeService;
   @InitBinder  
   public void initBinder(WebDataBinder binder) {  
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
       dateFormat.setLenient(false);  
       binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); 
   }
   /**
    * 添加贷款
    * @param loan
    * @return
    */
   @RequestMapping(value="/insert",method=RequestMethod.POST)
	public String insert(Loan loan,String username,HttpSession session){
	Custome custome=(Custome) session.getAttribute("custome") ;
	Custome cus=(Custome)customeService.selectByName(custome);
	loan.setCustomeid(cus.getCustomeid());
	System.out.println("cus"+cus);
	if(cus.getCustomedrive().equals(username)){
	loan.setCustomeid(cus.getCustomeid());
    Integer count=loanService.insert(loan);
    if(count>0){
	   return "redirect:/custome/user/find";
    }
    else
       return "/custome/loan/addLoan";
	}else{
		return  "/custome/loan/addLoan";
	}
   }
   /**
    * 跳转添加贷款页面
    *  
    * @return
    */
   @RequestMapping(value="/addLoan",method=RequestMethod.GET)
	public ModelAndView addLoan(){
       ModelAndView model=new ModelAndView("custome/addloan");
       List<Bank> list=bankService.select();
       model.addObject("bank",list);
       System.out.println("banklist"+list);	   
       return model;
   }
   /**
	   * 根据顾客编号修改信息
	   * @param session
	   * @return
	   */
	  @RequestMapping(value="/update",method=RequestMethod.POST)
			public ModelAndView update(Loan loan,HttpSession session){
		  ModelAndView  model;
		      Integer count=loanService.updateByPrimaryKeySelective(loan);
		      Custome cus=(Custome) session.getAttribute("custome");
	    	  Loan loan1=loanService.selectByPrimaryCustomeid(cus);
	    	  List<Bank> list=bankService.select();
	    	
		      if(count>0){
			   
		    	 model =new ModelAndView("custome/loandetail"); 
		    	  model.addObject("loan1",loan);
		    	  model.addObject("bank",list);
		      }
		      else{
			     
			      model =new ModelAndView("custome/updateloan");
			      model.addObject("loan1",loan);
		    	  model.addObject("bank",list);
			      model.addObject("msg","修改失败");
		      }
		      return model;
		    
			}
	  /**
	   * 顾客修改贷款信息
	   * @param session
	   * @return
	   */
	  @RequestMapping(value="/updateLoan",method=RequestMethod.GET)
		public ModelAndView updateCustome(HttpSession session){
		  Custome cus=(Custome) session.getAttribute("custome");
	      Custome custome=customeService.selectByCustomeId(cus.getCustomeid());
	      Loan loan=loanService.selectByPrimaryCustomeid(cus);
	      ModelAndView  model =new ModelAndView("custome/updateloan");
	     
	      List<Bank> list=bankService.select();
	      model.addObject("bank",list);
	      model.addObject("custome",custome);
	      model.addObject("loan",loan);
	      //model.addObject("custome",cus);
	      return model;
		}
	  /**
	   * 更新个证状态
	   * @param loanids
	   * @param session
	   * @return
	   */
	  @RequestMapping(value="/person",method=RequestMethod.POST)
		public String person(Integer[] loanids,Integer[] loanid1s,HttpSession session){
	     SysUser sysUser=(SysUser) session.getAttribute("sysUser");
	     System.out.println("sysUser"+sysUser);
	     
	     Integer count=loanService.updateByPrimaryLoanidPerson(loanids,loanid1s, sysUser);
	      return "redirect:/custome/user/findPerson1";
	   }
	  /**
	   * 修改汽证审核状态
	   * @param loanids
	   * @param loanid1s
	   * @param session
	   * @return
	   */
	  @RequestMapping(value="/car",method=RequestMethod.POST)
		public String car(Integer[] loanids,Integer[] loanid1s,HttpSession session){
	     SysUser sysUser=(SysUser) session.getAttribute("sysUser");
	     Integer count=loanService.updateByPrimaryLoanidCar(loanids,loanid1s, sysUser);
	      return "redirect:/custome/user/findCar1";
	   }
	  /**
	   * 修改保险审核状态
	   * @param loanids
	   * @param loanid1s
	   * @param session
	   * @return
	   */
	  @RequestMapping(value="/insure",method=RequestMethod.POST)
			public String insure(Integer[] loanids,Integer[] loanid1s,HttpSession session){
		     SysUser sysUser=(SysUser) session.getAttribute("sysUser");
		     Integer count=loanService.updateByPrimaryLoanidInsure(loanids,loanid1s, sysUser);
		      return "redirect:/custome/user/findInsure1";
		   }
	  /**
	   * 修改放款审核状态
	   * @param loanids
	   * @param loanid1s
	   * @param session
	   * @return
	   */
	  @RequestMapping(value="/success",method=RequestMethod.POST)
			public String success(Integer[] loanids,Integer[] loanid1s,HttpSession session){
		     SysUser sysUser=(SysUser) session.getAttribute("sysUser");
		     Integer count=loanService.updateByPrimaryLoanidSuccess(loanids,loanid1s, sysUser);
		    
		     return "redirect:/custome/user/findSuccess1";
		   }
	
	  /**
	   * 更新结果
	   * @param customedrive
	   *
	   * @return
	   */
	  @RequestMapping(value="/logsuccess",method=RequestMethod.POST)
			public String logsuccess(Loan loan,HttpSession session){
		     SysUser sysUser=(SysUser) session.getAttribute("sysUser");
		     System.out.println("loainsuccesss"+loan);
		     Integer count=loanService.updateByPrimaryKeySelective1(loan,sysUser);
			   return "redirect:/custome/user/findAlllog";
			     
				}
	
}
